<?php


namespace app\api\model\Goods;


use app\admin\model\Shop\ShopGoodsCateRelationModel;
use app\api\model\MerchantModel;
use think\facade\Db;
use think\Model;

class GoodsModel extends Model
{
    /**
     * 数据表名
     * @var string
     */
    protected $name = "goods";

    /**
     * 小店
     */
    public function merchant()
    {
        return $this->hasOne(MerchantModel::class, 'id', 'mer_id');
    }

    /**
     * 根据条件查询单条数据
     * @param $where
     * @param string $field
     * @return mixed
     * @throws
     */
    public static function getGoodsWhere($where, $field = "*")
    {
        $goods = self::field($field)->with(['merchant' => function ($query) {
            return $query->field('id,account,mer_name as nickname');
        }])->where($where)->find()->toArray();
        return $goods;
    }

    /**
     * 根据条件查询多条数据
     * @param $where
     * @return mixed
     * @throws
     */
    public static function getGoodsWhereMore($where, $filed = "*")
    {
        $list = Db::name("goods")->field($filed)->where($where)->select()->toArray();
        return $list;
    }

    /**
     * @return \think\model\relation\HasMany
     * 积分商城商品与分类关系表
     */
    public function cate_relation()
    {
        return $this->hasMany(ShopGoodsCateRelationModel::class, 'goods_id', 'id');
    }

    /**
     * 统计总数
     * @param $where
     * @return int
     */
    public static function getGoodsCountWhere($where)
    {
        $count = Db::name("goods")
            ->alias('g')
            ->join('goods_cate_relation gcr', 'gcr.goods_id = g.id')
            ->where($where)->count();
        return $count;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param array $where
     * @param int $sort
     * @param string $field
     * @return array
     * @throws
     */
    public static function getGoodsListWhere($page, $limit, $where = [], $sort = [], $field = "g.*")
    {
        $start = ceil(($page - 1) * $limit);
        $model = Db::name('goods')
            ->alias('g')
            ->join('goods_cate_relation gcr', 'gcr.goods_id = g.id')
            ->field($field)
            ->where($where);
        if (!empty($sort)) {
            //价格 (1价格升序 2价格降序)
            $sort['is_price'] = !empty($param['is_price']) ? $param['is_price'] : "";
            //销量(1销量升序 2销量降序)
            $sort['is_sales'] = !empty($param['is_sales']) ? $param['is_sales'] : "";
            if (!empty($sort['is_composite'])) {
                //综合
            } else {
                if ($sort['is_price'] == 1) {
                    $model->order('g.price desc');
                }
                if ($sort['is_price'] == 2) {
                    $model->order('g.price asc');
                }
                if ($sort['is_sales'] == 1) {
                    $model->order('g.sales desc');
                }
                if ($sort['is_sales'] == 2) {
                    $model->order('g.sales asc');
                }
            }
        } else {
            $model->order('g.sort desc');
        }
        $list = $model->limit($start, $limit)
            ->select()->toArray();
        return $list;
    }

    /**
     * 自增
     * @param $uid
     * @param $field
     * @param $amount
     * @return int
     * @throws
     */
    public static function updateGoodsInc($goods_id, $field, $amount)
    {
        $flag = Db::name('goods')->where([['id', '=', $goods_id]])->inc($field, $amount)->update();
        return $flag;
    }

    /**
     * 自减
     * @param $uid
     * @param $field
     * @param $amount
     * @return int
     * @throws
     */
    public static function updateGoodsDec($goods_id, $field, $amount)
    {
        $flag = Db::name('goods')->where([['id', '=', $goods_id]])->dec($field, $amount)->update();
        return $flag;
    }

    /**
     * 商品&销量变化
     * @param $goods_id
     * @param $num
     * @return bool
     * @throws
     */
    public static function goodsInOut($goods_id, $num)
    {
        $flag = Db::name('goods')->where([['id', '=', $goods_id]])->inc('sales', $num)->update();
        if (!$flag) {
            return false;
        }
        $flag = Db::name('goods')->where([['id', '=', $goods_id]])->dec('stock', $num)->update();
        if (!$flag) {
            return false;
        }
        return true;
    }

    /**
     * 获取随机几条数据
     * @param $num
     * @param $where
     * @return array|\PDOStatement|string|\think\Collection
     * @throws
     */
    public static function getRandomGoods($num, $where)
    {
        $list = Db::name('goods')->where($where)->count();
        $min = Db::name('goods')->where($where)->min('id');
        $max = $min + $list;
        if ($list < $num) {
            $num = $list;
        }
        $i = 1;
        $flag = 0;
        $ary = array();
        while ($i <= $num) {
            $rundnum = rand($min, $max);
            if ($flag != $rundnum) {
                if (!in_array($rundnum, $ary)) {
                    $ary[] = $rundnum;
                    $flag = $rundnum;
                } else {
                    $i--;
                }
                $i++;
            }
        }
        $data = Db::name('goods')->field("id,mer_id,good_name,img,label,price,vip_price,cost_price,group_price,postage,is_bargain,sales,ficti,browse")->where('id', 'in', $ary, 'or')->select()->toArray();
        return $data;
    }

}
